package org.example.aop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.example.entity.TbUser;
import org.example.service.ITbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;

@Aspect
@Component
public class LoginLogAop {
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    HttpServletRequest request;
    @Autowired
    ITbUserService iTbUserService;
    @Pointcut("@annotation(org.example.aop.Log2)")
    public void logs(){

    }
    @Around("logs()")
    public Object aop(ProceedingJoinPoint joinPoint) throws Throwable{
        String ip = request.getRemoteAddr();
        String username = (String) redisTemplate.opsForValue().get("username");
        LambdaQueryWrapper<TbUser> tbUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tbUserLambdaQueryWrapper.eq(TbUser::getUsername,username);
        TbUser one = iTbUserService.getOne(tbUserLambdaQueryWrapper);
        String posts = one.getPosts();//岗位
        String userAgent = request.getHeader("User-Agent");
        System.out.println("浏览器：=="+userAgent);
        InetAddress addr = InetAddress.getByName(ip);//登录地点
        if (userAgent.contains("Windows")) {
            System.out.println("系统为：Windows");
        } else if (userAgent.contains("Mac OS X")) {
            System.out.println("系统为：Mac");
        } else if (userAgent.contains("Linux")) {
            System.out.println("Linux");
        } else {
            System.out.println("未知");
        }
        return joinPoint.proceed();
    }
}
